<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    :append-to-body="true"
    @callBack="onCallBack"
    @close="onClose"
    @open="handleQuery"
  >
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" label-position="left">
      <el-form-item label="专业" prop="categoryIds">
        <Treeselect
          v-model="form.categoryIds"
          :multiple="true"
          :options="options"
          @input="onInput"
          placeholder="请选择专业分类"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getMajorCategoryIds, dispense } from '@/api/sysSkill'
import { getTree } from '@/api/sysMajorCategory'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  data() {
    return {
      dialogVisible: false,
      title: '分配',
      options: [],
      limit: 3,
      form: {
        userId: '',
        categoryIds: []
      },
      rules: {
        categoryIds: [
          {
            required: true,
            message: '请选择专业分类',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleQuery() {
      getMajorCategoryIds(this.form.userId).then(res => {
        if (res.code === 20000) {
          this.form.categoryIds = res.data
        }
      })
    },
    handlerTree() {
      getTree().then(res => {
        if (res.code === 20000 && res.data && res.data.length > 0) {
          this.options = res.data
        }
      })
    },
    onShow(id) {
      if (id) {
        this.form.userId = id
      }
      this.handlerTree()
      this.dialogVisible = true
    },
    onClose() {
      this.$refs['ruleForm'].resetFields()
      this.form = this.$options.data().form
      this.dialogVisible = false
    },
    onInput(n) {
      if (n.length > this.limit) {
        this.$message.error('最多可选择2条数据')
        delete n[n.length - 1]
      }
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          dispense(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.onCallBack()
              }
            })
            .catch((e) => {
            })
        } else {
          return false
        }
      })
    },
    onCallBack() {
      this.$emit('onCallBack')
    }
  }
}
</script>

<style scoped>
.el-form {
  padding: 20px 20px;
}

.main-select-el-tree .el-tree-node .is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}

.main-select-el-tree .el-tree-node.is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}
</style>
